package com.fs.sys.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fs.sys.bean.dto.InboxMsgDTO;
import com.fs.sys.bean.query.InboxMsgQuery;
import com.fs.sys.entityservice.InboxMsgEntityService;
import com.fs.common.annotations.Log;
import com.fs.common.bean.LogType;
import com.fs.common.bean.UserContextHolder;
import com.fs.ws.WebSocketMsg;
import com.fs.ws.WebSocketService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 站内信控制器 
 * @author Coder Generator 2025-06-06 11:03:12 
 **/
@RestController
@RequestMapping("/sys/inbox-msg")
@Slf4j
@Tag(name = "站内信控制器")
public class InboxMsgController {
    @Autowired
    private InboxMsgEntityService service;

    @Autowired
    private WebSocketService webSocketService;

    @PostMapping("add")
    @Operation(summary = "新增")
    @Log(value = "站内信", op = LogType.NEW)
    public void add(@RequestBody @Validated InboxMsgDTO req) {
        req = service.insert(req);
        String userId = req.getReceiverId();
        if (StringUtils.isNotBlank(userId)) {
            WebSocketMsg msg = new WebSocketMsg();
            msg.setType("inboxMsg");
            msg.setTitle(req.getTitle());
            msg.setContent(req.getContent());
            msg.setTime(LocalDateTime.now());
            msg.addParam("id", req.getId());
            webSocketService.sendToUser(userId, msg);
        }
    }

    @PutMapping("update")
    @Operation(summary = "更新")
    @Log(value = "站内信", op = LogType.EDIT)
    public void update(@RequestBody @Validated InboxMsgDTO req) {
        service.update(req);
    }

    @PostMapping("save")
    @Operation(summary = "保存")
    @Log(value = "站内信", op = LogType.EDIT)
    public InboxMsgDTO save(@RequestBody @Validated InboxMsgDTO req) {
        if (StringUtils.isBlank(req.getId())) {
            return service.insert(req);
        }
        service.update(req);
        return req;
    }

    @DeleteMapping("delete/{id}")
    @Operation(summary = "删除")
    @Log(value = "站内信", op = LogType.DELETE)
    public void delete(@PathVariable("id") String id) {
        service.delete(id);
    }

    @GetMapping("detail/{id}")
    @Operation(summary = "根据id查找记录")
    public InboxMsgDTO findById(@PathVariable("id") String id) {
        return service.findById(id).orElse(null);
    }

    @PostMapping("page-query")
    @Operation(summary = "查询-分页")
    public IPage<InboxMsgDTO> pageQuery(@RequestBody InboxMsgQuery query) {
        return service.pageQuery(query);
    }

    @PostMapping("query")
    @Operation(summary = "查询-不分页")
    public List<InboxMsgDTO> query(@RequestBody InboxMsgQuery query) {
        return service.query(query);
    }

    @PostMapping({"mine", "mine/page-query"})
    public IPage<InboxMsgDTO> mine(@RequestBody InboxMsgQuery query) {
        String userId = UserContextHolder.getUserIdOrThrow();
        query.setReceiverId(userId);
        return service.pageQuery(query);
    }

    @GetMapping("read")
    public void setRead(String id) {
        String userId = UserContextHolder.getUserIdOrThrow();
        service.updateBuilder()
                .set("status", 1)
                .set("read_time", LocalDateTime.now())
                .eq("receiver_id", userId)
                .eq("id", id)
                .update();
    }

    @GetMapping("read-all")
    public void setAllRead() {
        String userId = UserContextHolder.getUserIdOrThrow();
        service.updateBuilder()
                .set("status", 1)
                .set("read_time", LocalDateTime.now())
                .eq("receiver_id", userId)
                .update();
    }
}